﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AeonNexus.Crm.Workflow
{
    public static class Extensions
    {
        public static Dictionary<string, KeyValuePair<string, string>> ParseAttributes(this String attributes)
        {
            attributes = attributes.Trim().Replace(',', ';');
            var tempaAtt = attributes.Split(';');
            var AttributeList = new Dictionary<string, KeyValuePair<string,string>>();
            foreach (var att in tempaAtt)
            {
                var attribute = att.Split(':');
                AttributeList.Add(attribute[0].Replace("{", "").ToLower(), new KeyValuePair<string,string>(attribute[1].ToLower(), attribute[2].Replace("}", "").ToLower()));
            }
            return AttributeList;
        }

        public static string GenerateAggregate(this List<KeyValuePair<string, string>> attributes)
        {
            var retrieveAtt = new StringBuilder();
            foreach (var att in attributes)
            {
                retrieveAtt.AppendFormat("<attribute name='{0}' aggregate='{1}' alias='{0}_{1}' />", att.Key, att.Value);
            }
            return retrieveAtt.ToString();
        }

        public static string EntityStatusFetch(this string stateCodes, string attName)
        {
            if (stateCodes == null || string.IsNullOrEmpty(stateCodes.Trim()))
            {
                return string.Empty;
            }
            stateCodes = stateCodes.Trim().Replace(',', ';');
            var tempCodes = stateCodes.Split(';');
            var fetchCondition = new StringBuilder();
            fetchCondition.AppendFormat("<condition attribute=\"{0}\" operator=\"in\">", attName);
            foreach (var stateCode in tempCodes)
            {
                fetchCondition.AppendFormat("<value>{0}</value>", stateCode);
            }
            fetchCondition.Append("</condition>");
            return fetchCondition.ToString();
        }

    }
}
